﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace TextMIS
{
    public partial class frmDocEdit : Form
    {
        public frmDocEdit()
        {
            InitializeComponent();
        }

        public static string userName;
        public static string sex1 = "男";
        public static string sex2 = "女";
        public static string strSql;
        SqlConnection conn;
        SqlCommand com;
        SqlDataReader dr;

        private void frmDocEdit_Load(object sender, EventArgs e)
        {
            conn = new
SqlConnection(frmLogin.SQLstr);



            userName = frmLogin.userName;
            strSql = "select * from DoctorInfo  where Username = '" + userName + "'";
            com = new SqlCommand(strSql, conn);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            dr = com.ExecuteReader();

            if (dr.Read())
            {
                oldNum.Text = dr.GetValue(0).ToString();
                oldName.Text = dr.GetValue(1).ToString();
                oldSex.Text = dr.GetValue(2).ToString();
                oldJob.Text = dr.GetValue(3).ToString();
                oldBranch.Text = dr.GetValue(4).ToString();
            }
            conn.Close();
            txtNewNum.Text = oldNum.Text;
            txtNewName.Text = oldName.Text;
            cbxNewJob.Text = oldJob.Text;
            cbxNesBranch.Text = oldBranch.Text;
                rbtMan.Checked = true;
            txtNewNum.Enabled = false;
            txtNewName.Enabled = false;
            cbxNewJob.Enabled = false;
            cbxNesBranch.Enabled = false;
           // rbtMan.Enabled = false;

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtNewNum.Text = oldNum.Text;
            txtNewName.Text = oldName.Text;
            cbxNewJob.Text = oldJob.Text;
            cbxNesBranch.Text = oldBranch.Text;
            rbtMan.Checked = true;
            txtNewNum.Enabled = false;
            txtNewName.Enabled = false;
            cbxNewJob.Enabled = false;
            cbxNesBranch.Enabled = false;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (rbtMan.Checked)
            {
                strSql = "update DoctorInfo set DNo = '" +
       txtNewNum.Text.Trim() + "',Dname = '" + txtNewName.Text.Trim() + "',Dsex = '" +
       sex1 + "',Djob = '" + cbxNewJob.Text.Trim() + "',BranchNo = '" + cbxNesBranch.Text.Trim() + "'where Username = '" + userName + "'";
            }
            if(rbtWomen.Checked)
            {
                strSql = "update DoctorInfo set DNo = '" +
       txtNewNum.Text.Trim() + "',Dname = '" + txtNewName.Text.Trim() + "',Dsex = '" +
       sex2 + "',Djob = '" + cbxNewJob.Text.Trim() + "',BranchNo = '" + cbxNesBranch.Text.Trim() + "'where Username = '" + userName + "'";
                
            }
            com = new SqlCommand(strSql, conn);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            com.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show("修改成功！");

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmDocEdit_Click(object sender, EventArgs e)
        {
            txtNewNum.Enabled = true;
            txtNewName.Enabled = true;
            cbxNewJob.Enabled = true;
            cbxNesBranch.Enabled = true;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
